Many operating systems provide basic functions that can be invoked by programs to perform basic, low level tasks. For example, the function “malloc” can be invoked to cause memory to be dynamically allocated. These functions can be invoked many times during program execution; thus, it is important that these functions execute as efficiently as possible. Otherwise, overall program performance may suffer.
The functions provided by the operating system may be invoked by different programs in different execution contexts. For example, a function may be invoked by a program when that program is executing in single threaded mode. Similarly, a function may be invoked by a program when that program is executing in multi threaded mode. Because a function may be invoked by programs in both single and multi threaded modes, it has to accommodate both modes. This usually means that the function has to execute additional code to ensure that it is multi thread safe. Malloc, for example, implements locking code to derive locking functionality. This locking functionality ensures that multiple threads will not be allowed to write to the same memory region at the same time. This helps to ensure that data integrity and consistency are not compromised when a program is executing in multi threaded mode.
The problem with executing additional code to ensure multi thread safety is that it imposes additional overhead on the functions. This additional overhead is often incurred needlessly. For example, when a function is invoked by a program executing in single threaded mode, it does not need to ensure multi thread safety. Nevertheless, the additional code is executed anyway. This needless incurrence of the additional overhead causes the operation of the functions to not be optimized. Because these functions are invoked many times by many different programs, this non-optimized behavior can lead to significant program performance degradation.